package com.zyj.core.builder;

import com.zyj.core.config.Configuration;
import org.dom4j.Element;

import java.util.List;

/**
 * 专门用来解析映射文件
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析mapper配置文件
     *
     * @param rootElement
     */
    public void parseMapper(Element rootElement) {
        String namespace = rootElement.attributeValue("namespace");

        // TODO 映射文件中除了select标签还有很多其他的标签

        /**
         * <select id="queryUserByParams2"
         * 		resultType="com.zyj.mybatis.po.User"
         * 		statementType="prepared">
         * 		SELECT * FROM user WHERE sex = #{sex}
         * 	</select>
         */

        // 此处只实现了解析select标签，其他标签的解析尚未编写代码
        List<Element> selectElements = rootElement.elements("select");
        // 循环解析每一个select标签
        for (Element selectElement : selectElements) {
            // 使用XMLStatementBuilder构建器解析statement配置
            XMLStatementBuilder statementBuilder = new XMLStatementBuilder(configuration);
            /**
             * 解析单个statement
             */
            statementBuilder.parseStatement(namespace, selectElement);
        }

    }
}
